/**
 * 作者: chenlingzhi
 * 日期: 2024-12-13 14:05:21
 * 备注: 用户成长基金数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserGrowthFund;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.commondata.iap.GrowthFund;
import com.motu.monstercity.server.game.commondata.maintask.MainTaskConstant;
import com.motu.monstercity.server.game.logic.*;
import io.vertx.core.json.JsonArray;

import java.util.concurrent.ConcurrentHashMap;

public class UserGrowthFund extends UserGrowthFundModel {
	public static final byte YES = 1;
	public static final byte NO = 1;

	public UserGrowthFund() {
		super();
	}

	public UserGrowthFund(long userId, int type) {
		super();
		putUserId(userId);
		putType(type);
	}

	/**
	 * 是否重复购买
	 */
	public boolean isBuy(int groupId) {
		if (groupId == 1) {
			return isBuy1 == YES;
		} else if (groupId == 2) {
			return isBuy2 == YES;
		} else if (groupId == 3) {
			return isBuy3 == YES;
		}
		return false;
	}

	/**
	 * 设置购买状态
	 */
	public boolean setBuy(int groupId) {
		if (groupId == 1) {
			putIsBuy1(YES);
			return true;
		} else if (groupId == 2) {
			putIsBuy2(YES);
			return true;
		} else if (groupId == 3) {
			putIsBuy3(YES);
			return true;
		}
		return false;
	}

	/**
	 * 获取免费领奖状态
	 */
	public JsonArray getFreeRewardAry(int groupId) {
		String freeReward = null;
		if (groupId == 1) {
			freeReward = freeReward1;
		} else if (groupId == 2) {
			freeReward = freeReward2;
		} else if (groupId == 3) {
			freeReward = freeReward3;
		}
		if (freeReward == null || freeReward.isEmpty()) {
			return new JsonArray();
		}
		return new JsonArray(freeReward);
	}

	/**
	 * 设置免费领奖状态
	 */
	public void setFreeReward(String freeReward, int groupId) {
		if (groupId == 1) {
			putFreeReward1(freeReward);
		} else if (groupId == 2) {
			putFreeReward2(freeReward);
		} else if (groupId == 3) {
			putFreeReward3(freeReward);
		}
	}

	/**
	 * 获取付费领奖状态
	 */
	public JsonArray getPayRewardAry(int groupId) {
		String payReward = null;
		if (groupId == 1) {
			payReward = payReward1;
		} else if (groupId == 2) {
			payReward = payReward2;
		} else if (groupId == 3) {
			payReward = payReward3;
		}
		if (payReward == null || payReward.isEmpty()) {
			return new JsonArray();
		}
		return new JsonArray(payReward);
	}

	/**
	 * 设置付费领奖状态
	 */
	public void setPayReward(String payReward, int groupId) {
		if (groupId == 1) {
			putPayReward1(payReward);
		} else if (groupId == 2) {
			putPayReward2(payReward);
		} else if (groupId == 3) {
			putPayReward3(payReward);
		}
	}

	/**
	 * 成长基金类型:目前已有5个类型
	 * @return
	 */
	public int[] getGrowthFund() {
		int[] GROWTH_FUND = null;
		if (type == MainTaskConstant.TASK_TYPE_113) {
			GROWTH_FUND = AllParam.GROWTH_FUND_113;
		} else if (type == MainTaskConstant.TASK_TYPE_144) {
			GROWTH_FUND = AllParam.GROWTH_FUND_144;
		} else if (type == MainTaskConstant.TASK_TYPE_184) {
			GROWTH_FUND = AllParam.GROWTH_FUND_184;
		} else if (type == MainTaskConstant.TASK_TYPE_141) {
			GROWTH_FUND = AllParam.GROWTH_FUND_141;
		} else if (type == MainTaskConstant.TASK_TYPE_117) {
			GROWTH_FUND = AllParam.GROWTH_FUND_117;
		}
		return GROWTH_FUND;
	}


	public PBUserGrowthFund.Builder takeInitBuilder() {
		int[] GROWTH_FUND = getGrowthFund();
		PBUserGrowthFund.Builder builder = PBUserGrowthFund.newBuilder();
		for (int groupId = 1; groupId <= GROWTH_FUND.length; groupId++) {
			ConcurrentHashMap<Long, GrowthFund> growthFundGroupMap = IapManager.getGrowthFundGroupMap(type,groupId);
			if (groupId == GROWTH_FUND.length
					|| getFreeRewardAry(groupId).size() < growthFundGroupMap.size()
					|| !isBuy(groupId)
					|| getPayRewardAry(groupId).size() < growthFundGroupMap.size()) {
				builder.setGroupId(groupId);
				builder.setType(type);
				builder.setIsBuy(isBuy(groupId));
				JsonArray freeRewardAry = getFreeRewardAry(groupId);
				for (int i = 0; i < freeRewardAry.size(); i++) {
					builder.addFreeReward(freeRewardAry.getInteger(i));
				}
				JsonArray payRewardAry = getPayRewardAry(groupId);
				for (int i = 0; i < payRewardAry.size(); i++) {
					builder.addPayReward(payRewardAry.getInteger(i));
				}
				break;
			}
		}
		return builder;
	}

	public PBUserGrowthFund.Builder takeBuilder() {
		return takeInitBuilder();
	}

}
